package com.kongge.viewdisplayframeworklib.model;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import com.kongge.viewdisplayframeworklib.event.param.EQParam;
import com.kongge.viewdisplayframeworklib.landscape.ISavePageState;
import com.kongge.viewdisplayframeworklib.ui.Component;
import com.kongge.viewdisplayframeworklib.ui.ComponentContainer;

import java.util.Vector;

public class Page extends AbsUIController{


	private Vector<Component> components;
	private ComponentContainer componentContainer;
	


	public Page(PageNode pageNode, ViewGroup viewGroup) {
		// TODO Auto-generated constructor stub
		super(pageNode, viewGroup);
		
		components = new Vector<Component>();
		
		findComponent(mViewGroup);
		findComponentContainer(mViewGroup);
		dispatchEvent(EVENT_ONPAGEFINISHLOAD);
	}



	@Override
	public void dispatchParam(EQParam param) {
		param.page = this;

		for (Component com : components) {
			com.parseRunTimeParam(param);
			Log.i("testLog", "parseRunTimeParam:" + com.getClass().getSimpleName());
		}
	}

	@Override
	public void dispatchEvent(int event) {
		if (componentContainer != null && event == EVENT_ONFOREGOUND) {
			componentContainer.onComponentContainerForeground();
			Log.i("testLog", "onComponentContainerForeground:" + componentContainer.getClass().getSimpleName());
		}
		if (components != null) {
			int count = components.size();
			for (int i = 0; i < count; i++) {
				Component component = components.elementAt(i);
				switch (event) {
				case EVENT_ONFOREGOUND:
					component.onForeground();
					Log.i("testLog", "onForeground:" + component.getClass().getSimpleName());
					break;
				case EVENT_ONBACKGROUND:
					component.onBackground();
					Log.i("testLog", "onBackground:" + component.getClass().getSimpleName());
					break;
				case EVENT_ONPAGEFINISHLOAD:
					component.onPageFinishInflate();
					Log.i("testLog", "onPageFinishInflate:" + component.getClass().getSimpleName());
					break;
				case EVENT_ONREMOVE:
					component.onRemove();
					Log.i("testLog", "onRemove:" + component.getClass().getSimpleName());
					break;
				default:
					break;
				}
			}
		}

		if (componentContainer != null && event == EVENT_ONBACKGROUND) {
			componentContainer.onComponentContainerBackground();
			Log.i("testLog", "onComponentContainerBackground:" + componentContainer.getClass().getSimpleName());
		}

		if (componentContainer != null && event == EVENT_ONREMOVE) {
			componentContainer.onComponentContainerRemove();
			Log.i("testLog", "onComponentContainerRemove:" + componentContainer.getClass().getSimpleName());
		}
	}

	private void findComponent(ViewGroup viewGroup) {
		if (viewGroup == null) {
			return;
		}
		if (viewGroup instanceof Component) {
			components.add((Component)viewGroup);
			return;
		}
		int count = viewGroup.getChildCount();
		for (int i = 0; i < count; i++) {
			View child = viewGroup.getChildAt(i);
			if (child instanceof Component) {
				Component component = (Component)child;
				components.add(component);
			} else if (child instanceof ViewGroup) {
				findComponent((ViewGroup) child);
			}
		}
	}
	
	private void findComponentContainer(ViewGroup viewGroup) {
		if (viewGroup == null) {
			return;
		}
		if (viewGroup instanceof ComponentContainer) {
			componentContainer = (ComponentContainer)viewGroup;
			return;
		}
		int count = viewGroup.getChildCount();
		for (int i = 0; i < count; i++) {
			View child = viewGroup.getChildAt(i);
			if (child instanceof Component) {
				componentContainer = (ComponentContainer) viewGroup;
				return;
			} else if (child instanceof ViewGroup) {
				findComponent((ViewGroup) child);
			}
		}
		
	}

	@Override
	public boolean isBottomBarVisible() {
		if (componentContainer != null) {
			return componentContainer.getBottomVisible();
		}
		return true;
	}

	@Override
	public NavigateItem getNavigateItem() {
		if (componentContainer != null) {
			return componentContainer.getTitleStruct();
		}
		return null;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			if (componentContainer != null) {
				return componentContainer.onInnerBack();
			}
		}
		return false;
	}

	@Override
	public void broadcastScreenChange() {
		Vector<Component> tempComponents = new Vector<Component>();
		tempComponents = components;
		int count = tempComponents.size();
		for (int i = 0; i < count; i++) {
			Component component = tempComponents.elementAt(i);
			if (component instanceof ISavePageState) {
				((ISavePageState)component).savePageState();
			}
		}
	}

}
